//
//  Permission+Photos.swift
//  AnyImageKit
//
//  Created by 刘栋 on 2020/1/7.
//  Copyright © 2020 AnyImageProject.org. All rights reserved.
//

import Photos

extension Permission {
    
    func _checkPhotos() -> Status {
        return PHPhotoLibrary.authorizationStatus()._status
    }
    
    func _requestPhotos(completion: @escaping PermissionCompletion) {
        guard let _ = Bundle.main.object(forInfoDictionaryKey: ._photoLibraryUsageDescription) else {
            _print("WARNING: \(String._photoLibraryUsageDescription) not found in Info.plist")
            return
        }
        
        PHPhotoLibrary.requestAuthorization { status in
            Thread.runOnMain {
                completion(status._status)
            }
        }
    }
}

fileprivate extension PHAuthorizationStatus {
    
    var _status: Permission.Status {
        switch self {
        case .notDetermined:
            return .notDetermined
        case .authorized:
            return .authorized
        default:
            return .denied
        }
    }
}

fileprivate extension String {
    
    static let _photoLibraryUsageDescription: String = "NSPhotoLibraryUsageDescription"
}
